Deep copy (deep copy) and shallow copy (shallow copy) are two more common concepts, especially in the C + + language, if not understood, it will be in the delete when the problem, but we are fortunate to use
Deep copy (deep copy) and shallow copy (shallow copy) are two more general concepts, especially in the C + + language, which can be problematic when the delete is not understood, but we are fortunately using
Landlord is a asp.net siege lions, recently ran Java Group Guest to help develop, so recently some basic knowledge of Java special attention. There is a situation where you need to copy a object deeply to do something else, and the original object remains in its original state. (really do not want to new one out, and then to a pile of fields to assign value ...)
The copy of programming languages can be divided into reference copy, shallow copy and deep copy.
It is relatively simple to reference copy. We direct a reference to an object, that is, a reference to
not be very frequent in the development of a project, but distinguishing between deep and shallow copies gives us a deeper understanding of how Java memory is structured and run. As for thorough deep copy, it is almost impossible to achieve, for reasons already described in the previous section.
copy, and the third is a deep copy. so you know, these three kinds of concepts are actually for copying objects.1, directly assigned valueOK, let's look at the first way, directly assign values . In Java, A a1 = A2, what we need to understand is that this is actually a reference, which means that A1 and A2 point to th
("=================================");
System.out.println ("After clone,c2.str =" + c2.str);
System.out.println ("After clone,c2.strbuff =" + C2.strbuff);
}
}
Execution results:
As you can see from the printed results, the string variable seems to have implemented a deep clone because the C2.STR changes did not affect the c1.str! Does Java think of sring classes as basic data ty
1. ConceptThe clone in Java is divided into:A: Shallow copy (Shallow clone): Shallow copy copies only the objects that you are considering, not the objects that it refers to.B: Deep copy (Deep clone):
First, what is it?Shallow copy : Copy the value of a member variable of a value type , copy only the reference to a member variable of the reference type, and do not copy the referenced object deep copy: a
another.student.school.schoolName = "Wuda"; System.out.println (Copy.student.school.schoolName);//nongda}} class copy implements Serializable {public Strin
G name;
public int age;
Public Student Student;
Public Copy (String name,int age,student Student) {this.name = name;
This.age = age;
This.student = student;
} class Student implements Serializable {public int score;
Public Co
();} return null;} Public Student Deepclone () {//Uses clone () to create the object, deep copy try{student cloning = (Student) super.clone ();//student cloning = ( strdent) This.clone ();//And the previous sentence effect equivalent cloning.courses = new Vector ();//Key: The non-basic data type of space needs to be newly opened together return cloning;} catch (Clonenotsupportedexception e) {e.printstacktr
Recently read some books, deep copy and shallow copy of the words, always appear in front of the eyes, and then organized a bit, about the principle and implementation of the depth of Java.1. What is a deep/shallow copy?Shallow
call Private void throws ioexception{ stream.defaultwriteobject (); Stream.writeobject (custom object);} // when the object is deserialized, if this method is implemented, the call Private void throws ioexception{ stream.defaultreadobject (); Stream.readobject ();}Note: Neither clone nor serialization calls the object's construction method;Serialization does not serialize static and transient-modified variables;Code reference: Shallow copy
1. Shallow copy and deep copy Concepts ⑴ Shallow copy (shallow clone) All variables of a replicated object contain the same value as the original object , and all references to other objects still point to the original object. In other words, a shallow copy replica
in Java incorporate the object class by default, and a clone () method is provided in the object class. The purpose of this method is to return a copy of an object. This copy function returns a new object instead of an application. So how do you use this method? The following are steps for using the Clone () method.
1, the implementation of the Clone () class, t
Original blog: http://blog.csdn.net/qq_29329775/article/details/49516247There has been a recent error in doing algorithm jobs because there is no clear distinction between deep replication and shallow replication of Java collections.1. First, the understanding of shallow copy and deep
Any class in Java that implements the Cloneable interface can replicate a copy of itself and pass it to the caller by calling the Clone () method. Generally, the clone () method satisfies:(1) For any object x, there is X.clone ()!=x, that is, the cloned object is not the same object as the original object.(2) For any object x, there is X.clone (). GetClass () ==x.getclass (), that is, the Clone object is th
later you change the field of the class (increase or decrease or rename), when you deserialize, there will be exception, which will cause incompatibility problems. //But when the serialversionuid is the same, it assigns a different field to the default value of type (such as the int type is the 0,string type is NULL, etc.), which avoids the problem of incompatibility. So it's best to assign Serialversionuid Private Static Final LongSerialversionuid = 7991552226614088458L; Public intAge ; P
= person;//Reference Assignment8 }9 Ten PublicAsian (Asian Asian) {//Copy construction methods, copying objects One This(Asian.skin,asian.person); A } -}The above object also has two member variables, skin and person objectsFor the person object there are the following:1 New Person ("John Doe", "mam", +); 2 3 New Person (P1);When The above statement is called. The P2 object will replicate to the P1. The implementation is as f
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.